package com.ruoyi.lxzx.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.lxzx.appDTO.lxlbDTO.SearchDTO;
import com.ruoyi.lxzx.appDTO.lxlbDTO.UserCardDto;
import com.ruoyi.lxzx.appVO.lxjhVO.BmrVO;
import com.ruoyi.lxzx.appVO.lxlbVO.LxlbListDetailVO;
import com.ruoyi.lxzx.appVO.lxlbVO.LxlbListVO;
import com.ruoyi.lxzx.service.IUserAttentService;
import com.ruoyi.system.service.ISysUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.lxzx.mapper.UserCardMapper;
import com.ruoyi.lxzx.domain.UserCard;
import com.ruoyi.lxzx.service.IUserCardService;

/**
 * 用户名片Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-26
 */
@Service
@AllArgsConstructor
public class UserCardServiceImpl implements IUserCardService {
    private UserCardMapper userCardMapper;
    private IUserAttentService userAttentService;

    /**
     * 查询用户名片
     *
     * @param id 用户名片主键
     * @return 用户名片
     */
    @Override
    public UserCard selectUserCardById(Long id) {
        return userCardMapper.selectUserCardById(id);
    }

    /**
     * 查询用户名片列表
     *
     * @param userCard 用户名片
     * @return 用户名片
     */
    @Override
    public List<UserCard> selectUserCardList(UserCard userCard) {
        return userCardMapper.selectUserCardList(userCard);
    }

    /**
     * 新增用户名片
     *
     * @param userCard 用户名片
     * @return 结果
     */
    @Override
    public int insertUserCard(UserCard userCard) {
        userCard.setCreateTime(DateUtils.getNowDate());
        return userCardMapper.insertUserCard(userCard);
    }

    @Override
    public boolean updateUserCardApp(UserCardDto userCard) {
        ISysUserService userService = SpringUtils.getBean(ISysUserService.class);
        SysUser sysUser = userService.findDistinctSysUser(userCard.getName(), userCard.getTel());
//        if(sysUser == null) {
            userCard.setIsCard("1");
            userCard.setUpdateTime(DateUtils.getNowDate());
            SysUser su = new SysUser();
            su.setUserId(userCard.getUserId() == null ? SecurityUtils.getUserId() : userCard.getUserId());
            su.setNickName(userCard.getName());
            su.setPhonenumber(userCard.getTel());
            userService.updateUseById(su);
            UserCard uc = new UserCard();
            BeanUtils.copyProperties(userCard, uc);
            userCardMapper.updateUserCard(uc);
            return true;
//        }else {
//            return false;
//        }
    }

    /**
     * 修改用户名片
     *
     * @param userCard 用户名片
     * @return 结果
     */
    @Override
    public int updateUserCard(UserCard userCard) {
        userCard.setIsCard("1");
        userCard.setUpdateTime(DateUtils.getNowDate());
        return userCardMapper.updateUserCard(userCard);
    }
    /**
     * 删除用户名片标志
     *
     * @param userCard 备注
     * @return 结果
     */
    @Override
    public int updateUserRemoveCard(UserCard userCard) {

        return userCardMapper.updateUserRemoveCard(userCard);
    }

    /**
     * 批量删除用户名片
     *
     * @param ids 需要删除的用户名片主键
     * @return 结果
     */
    @Override
    public int deleteUserCardByIds(Long[] ids) {

        return userCardMapper.deleteUserCardByIds(ids);
    }

    /**
     * 删除用户名片信息
     *
     * @param id 用户名片主键
     * @return 结果
     */
    @Override
    public int deleteUserCardById(Long id) {
        return userCardMapper.deleteUserCardById(id);
    }

    @Override
    public List<LxlbListVO> lxlb(SearchDTO param) {
        List<LxlbListVO> result = userCardMapper.lxlb(param);
        Long userId = null;
        try {
            userId = SecurityUtils.getUserId();
        } catch (Exception e) {
        }
        if (userId != null) {
            Long finalUserId = userId;
            result.forEach(r -> {
                if(finalUserId.equals(r.getUserId())) {
                    r.setIsOwn("1");
                }else {
                    r.setGzStatus(userAttentService.gzStatus(r.getUserId(), finalUserId));
                }
            });
        }else {
            result.forEach(r -> {
                r.setGzStatus("0");
            });
        }
        return result;
    }

    @Override
    public LxlbListDetailVO ckmp(Long userId) {
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
        }
        LxlbListDetailVO result = userCardMapper.ckmp(userId);
        if(result == null){
            throw  new ServiceException("没有获取到对应信息");
        }
        if(result != null) {
            if (finalUserId.equals(userId) || 0 == finalUserId) {
                result.setExchangeFlag(false);
            }else {
                result.setExchangeFlag(true);
                result.setGzStatus(userAttentService.gzStatus(userId, finalUserId));
            }
        }else {
            result.setExchangeFlag(false);
            result.setGzStatus("0");
        }
        return result;
    }

    @Override
    public String hqdz(Long userId) {
        return userCardMapper.hqdz(userId);
    }

    @Override
    public List<UserCard> findUserCardList(Long activityId) {
        return userCardMapper.findUserCardList(activityId);
    }

    @Override
    public LxlbListDetailVO findUserCardByName(String createBy) {
        return userCardMapper.findUserCardByName(createBy);
    }

    @Override
    public LxlbListDetailVO dlckmp() {
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
            throw new ServiceException("当前未登录,或登录已过期");
        }
        LxlbListDetailVO result = userCardMapper.ckmp(finalUserId);

        result.setExchangeFlag(false);
        //后期需要删掉
        result.setIsReal("2");
        return result;
    }

    @Override
    public List<BmrVO> hdjhybmr(Long activityId) {
        return userCardMapper.hdjhybmr(activityId);
    }

    @Override
    public UserCard findByUserId(Long userId) {
        return userCardMapper.findByUserId(userId);
    }

    @Override
    public List<LxlbListVO> sqgl(Long userId) {
        List<LxlbListVO> result = userCardMapper.sqgl(userId);
        result.forEach(r -> {
            if (userId.equals(r.getUserId())) {
                r.setIsOwn("1");
            } else {
                r.setGzStatus(userAttentService.gzStatus(r.getUserId(), userId));
            }
        });
        return result;
    }

    @Override
    public List<LxlbListVO> wdgz(Long userId) {
        List<LxlbListVO> result = userCardMapper.wdgz(userId);
        result.forEach(r -> {
            if (userId.equals(r.getUserId())) {
                r.setIsOwn("1");
            } else {
                r.setGzStatus(userAttentService.gzStatus(r.getUserId(), userId));
            }
        });
        return result;
    }

    @Override
    public void gxxjd(String addressPresent, Long finalUserId) {
        userCardMapper.gxxjd(addressPresent,finalUserId);
    }

    @Override
    public void xgtx(String img) {
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
            throw new ServiceException("当前未登录,或登录已过期");
        }
        userCardMapper.xgtx(finalUserId,img);
    }

}
